package com.yjx.gulimall.coupon.service.impl;

import com.yjx.gulimall.common.vo.product.MemberPrice;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;

import com.yjx.gulimall.coupon.dao.MemberPriceDao;
import com.yjx.gulimall.coupon.entity.MemberPriceEntity;
import com.yjx.gulimall.coupon.service.MemberPriceService;
import org.springframework.util.CollectionUtils;


@Service("memberPriceService")
public class MemberPriceServiceImpl extends ServiceImpl<MemberPriceDao, MemberPriceEntity> implements MemberPriceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberPriceEntity> page = this.page(
                new Query<MemberPriceEntity>().getPage(params),
                new QueryWrapper<MemberPriceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBatchInfo(Long skuId, List<MemberPrice> memberPrice) {

        if (!CollectionUtils.isEmpty(memberPrice)) {
            List<MemberPriceEntity> memberPriceEntities = memberPrice.stream()
                    .filter(memberPrice1 -> memberPrice1.getPrice().compareTo(new BigDecimal("0")) == 1)
                    .map(memberPrice1 -> {
                        MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                        memberPriceEntity.setSkuId(skuId);
                        memberPriceEntity.setMemberLevelId(memberPrice1.getId());
                        memberPriceEntity.setMemberLevelName(memberPrice1.getName());
                        memberPriceEntity.setMemberPrice(memberPrice1.getPrice());
                        memberPriceEntity.setAddOther(1);
                        return memberPriceEntity;
                    }).collect(Collectors.toList());
                this.saveBatch(memberPriceEntities);
        }
    }

}